package com.dhu.stylefit.service;

import com.dhu.stylefit.dao.UserInfo;
import com.dhu.stylefit.dao.UserInfoRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    private final UserInfoRepository userInfoRepository;

    @Autowired
    public UserInfoServiceImpl(UserInfoRepository userInfoRepository) {
        this.userInfoRepository = userInfoRepository;
    }

    @Override
    public List<UserInfo> findAll() {
        return userInfoRepository.findAll();
    }

    @Override
    public Optional<UserInfo> findById(Integer userId) {
        return userInfoRepository.findById(userId);
    }

    @Override
    public UserInfo save(UserInfo userInfo) {
        return userInfoRepository.save(userInfo);
    }

    @Override
    public void deleteById(Integer userId) {
        userInfoRepository.deleteById(userId);
    }

    @Override
    @Transactional
    public UserInfo updateUserInfo(UserInfo updatedInfo) {
        // 1. 验证输入参数
        if (updatedInfo == null || updatedInfo.getUserId() == null) {
            throw new IllegalArgumentException("Invalid UserInfo or userId is null");
        }

        // 2. 检查用户是否存在
        Optional<UserInfo> userOptional = userInfoRepository.findById(updatedInfo.getUserId());
        if (!userOptional.isPresent()) {
            throw new RuntimeException("User with id " + updatedInfo.getUserId() + " not found");
        }

        // 3. 获取现有用户信息
        UserInfo existingInfo = userOptional.get();

        // 4. 更新头像URL
        if (updatedInfo.getAvatarUrl() != null) {
            existingInfo.setAvatarUrl(updatedInfo.getAvatarUrl());
        }

        // 5. 更新模特照片URL（包含旧文件删除逻辑）
        if (updatedInfo.getModelPhotoUrl() != null) {
            existingInfo.setModelPhotoUrl(updatedInfo.getModelPhotoUrl());
        }

        // 6. 更新其他基本信息
        if (updatedInfo.getAge() != null) {
            if (updatedInfo.getAge() < 0 || updatedInfo.getAge() > 150) {
                throw new IllegalArgumentException("Invalid age value");
            }
            existingInfo.setAge(updatedInfo.getAge());
        }

        if (updatedInfo.getGender() != null) {
            existingInfo.setGender(updatedInfo.getGender());
        }

        if (updatedInfo.getHeight() != null) {
            if (updatedInfo.getHeight() <= 0 || updatedInfo.getHeight() > 300) {
                throw new IllegalArgumentException("Invalid height value");
            }
            existingInfo.setHeight(updatedInfo.getHeight());
        }

        if (updatedInfo.getWeight() != null) {
            if (updatedInfo.getWeight() <= 0 || updatedInfo.getWeight() > 500) {
                throw new IllegalArgumentException("Invalid weight value");
            }
            existingInfo.setWeight(updatedInfo.getWeight());
        }

        if (updatedInfo.getAddress() != null) {
            if (updatedInfo.getAddress().length() > 255) {
                throw new IllegalArgumentException("Address too long");
            }
            existingInfo.setAddress(updatedInfo.getAddress());
        }

        // 7. 保存更新后的用户信息
        return userInfoRepository.save(existingInfo);
    }
}
